<?php
	//引入配置文件和数据操作函数
	require '../../public/config.php';
	require '../../public/mysql.php';
	require '../../public/function.php';
	//获取传递过来的操作
	$action = $_GET['a'];

	//判断操作
	switch ($action) {
		//添加商品
		case 'add':
			//接受表单传递的数据
			$goodsName = $_POST['name'];
			$cid = $_POST['cid'];
			$price = $_POST['price'];
			$company = $_POST['company'];
			$store = $_POST['store'];
			$descr = $_POST['descr'];
			$is_sale = $_POST['is_sale'];
			$is_hot = $_POST['is_hot'];
			$is_new = $_POST['is_new'];
			$addtime = time();

		//对数据进行验证
              $preg='/\S+/';
				 if(@!preg_match($preg,$goodsName)){
				         echo "<script>alert('商品名称不能为空！');location.href='./goodsAdd.php'</script>";
                       die();
				 }  
				 $pregprice = '/^\d+$/';
				 if(@!preg_match($pregprice,$price)){
				         echo "<script>alert('商品价格必须为数字！');location.href='./goodsAdd.php'</script>";
                     die();
				 }  
				 $pregcompany='/\S+/';
				 if(@!preg_match($pregcompany,$company)){
				         echo "<script>alert('出产地不能为空');location.href='./goodsAdd.php'</script>";
                       die();
				 }  

				 $pregstore='/^\d+$/';
				 if(@!preg_match($pregstore,$store)){
				         echo "<script>alert('库存必须为数字！');location.href='./goodsAdd.php'</script>";
                       die();
				 } 

			//图片需要单独处理
			$file = $_FILES['pic'];
			//图片不能为空
			if ($file['error'] == 4) {
				echo "<script>alert('图片不能为空');location.href='./goodsAdd.php'</script>";
				die();
			}
			//执行图片的上传
			//图片的保存路径
			$path = '../../public/upload';
			//允许上传的文件类型
			$alloType = array('image/jpeg', 'image/png','image/gif');
			$imageInfo = uploaded($file,$path,$alloType);
			//执行缩放
			if ($imageInfo['error']) {
				//执行缩放的操作
				//找到上传的文件
				$imagePath = $path.'/'.$imageInfo['info'];
				//调用缩放的函数
			}

			var_dump($imageInfo);
			if ($imageInfo['error']) {
				//获取图片中路径信息
				$pic = $imageInfo['info'];
				//准备sql语句
				$sql = "insert into goods(name,cid,price,company,dsecr,pic,store,is_hot,is_sale,is_new,addtime) values('{$goodsName}','{$cid}','{$price}','{$company}','{$descr}','{$pic}','{$store}','{$is_hot}','{$is_sale}','{$is_new}','{$addtime}')";
				echo $sql;
				//连接数据库
				$link = connect();
				//执行插入的操作
				if ($link['error']) {
					//执行操作
					$result = excute($link['mess'], $sql);
					if ($result['error']) {
						echo "<script>alert('添加成功');location.href='./goodsList.php'</script>";
					} else {
						//如果数据插入失败，需要将图片删除
						$path = '../../public/upload/'.$pic;
						@unlink($path);
						echo "<script>alert('请重新添加数据');location.href='./goodsAdd.php'</script>";
					}
				}
			}
		break;

		//修改商品
		case 'edit':
		//接受表单传递的数据
		$gid = $_POST['gid'];
		$goodsName = $_POST['name'];
		$cid = $_POST['cid'];
		$price = $_POST['price'];
		$company = $_POST['company'];
		$store = $_POST['store'];
		$descr = $_POST['descr'];
		$is_sale = $_POST['is_sale'];
		$is_hot = $_POST['is_hot'];
		$is_new = $_POST['is_new'];

		//对数据进行验证
          $preg='/\S+/';
				 if(@!preg_match($preg,$goodsName)){
				         echo "<script>alert('商品名称不能为空！');location.href='./goodsList.php'</script>";
                       die();
				 }  
				 $pregprice = '/^\d+$/';
				 if(@!preg_match($pregprice,$price)){
				         echo "<script>alert('商品价格必须为数字！');location.href='./goodsList.php'</script>";
                     die();
				 }  
				 $pregcompany='/\S+/';
				 if(@!preg_match($pregcompany,$company)){
				         echo "<script>alert('出产地不能为空');location.href='./goodsList.php'</script>";
                       die();
				 }  

				 $pregstore='/^\d+$/';
				 if(@!preg_match($pregstore,$store)){
				         echo "<script>alert('库存必须为数字！');location.href='./goodsAdd.php'</script>";
                       die();
				 } 
		//图片需要单独处理
		$file = $_FILES['pic'];
		//var_dump($file);
		//判断是否传递了图片
		if ($file['error'] !=4) {
			//上传了图片了
			//执行图片的上传
			//图片的保存路径
			$path = '../../public/upload';
			//允许上传的文件类型
			$alloType = array('image/jpeg', 'image/png','image/gif');
			$imageInfo = uploaded($file,$path,$alloType);

			//如果上传成功执行修改图片的操作
			if ($imageInfo['error']) {
				//连接数据库
				$link = connect();
				//得到旧图的信息
				if ($link['error']) {
					$sql = "select pic from goods where id ='{$gid}'";
					$result = query($link['mess'], $sql);
				}
				$old_image = $result[0]['pic'];
				
				//准备sql,修改数据
				if ($link['error']) {
					$newPic = $imageInfo['info'];
					$sql = "update goods set name='{$goodsName}',cid='{$cid}',pic='{$newPic}',price='{$price}',company='{$company}',store='{$store}',dsecr='{$descr}',is_sale='{$is_sale}',is_hot='{$is_hot}',is_new='{$is_new}' where id='{$gid}'";
					$result = excute($link['mess'], $sql);
					if ($result['error']) {
						//删除掉旧的图片
						$path = '../../public/upload/'.$old_image;
						@unlink($path);
						echo "<script>alert('修改成功');location.href='./goodsList.php'</script>";
					} else {
						//如果数据没有修改成功
						$path = '../../public/upload/'.$newPic;
						@unlink($path);
						echo "<script>alert('请重新修改');location.href='./goodsEdit.php?id='".$gid."';</script>";
					}
				}
			}
			die();
			
		}
		//修改基本信息的操作
		//准备sql
		$sql = "update goods set name='{$goodsName}',cid='{$cid}',price='{$price}',company='{$company}',store='{$store}',dsecr='{$descr}',is_sale='{$is_sale}',is_hot='{$is_hot}',is_new='{$is_new}' where id='{$gid}'";
		//执行sql
		$link = connect();
		if ($link['error']) {
			$result = excute($link['mess'], $sql);
			if ($result['error']) {
				echo "<script>alert('修改成功');location.href='./goodsList.php'</script>";
			}
		}
		break;

		//删除操作
		case 'del':
			//接受商品的id
			$id = $_GET['id'];
			//var_dump($id);
			//需要将该条商品的图片信息查询出来
			//连接数据库
			$link = connect();
			//得到旧图的信息
			if ($link['error']) {
				$sql = "select pic from goods where id ='{$id}'";
				$result = query($link['mess'], $sql);
			}
			$old_image = $result[0]['pic'];
			//执行删除的操作
			if ($link['error']) {
				$sql = "delete from goods where id ='{$id}'";
				$result = excute($link['mess'], $sql);
				//判断是否删除成功
				if ($result['error']) {
					//删除掉旧的图片
					$path = '../../public/upload/'.$old_image;
					@unlink($path);
					echo "<script>alert('删除成功');location.href='./goodsList.php'</script>";
					die();
				}
			}
			echo "<script>alert('删除失败');location.href='./goodsList.php'</script>";
		break;
		//添加多图
	case 'addImg':
			$id = $_POST['gid'];
			$file = $_FILES['image'];
			if ($file['error'][0] == 4) {
				echo "<script>alert('图片不能为空');location.href='./imageList.php?id=$id'</script>";
				die();
			}
			foreach ($file['name'] as $key => $val) {
				$image['name'] = $file['name'][$key];
				$image['type'] = $file['type'][$key];
				$image['tmp_name'] = $file['tmp_name'][$key];
				$image['error'] = $file['error'][$key];
				$image['size'] = $file['size'][$key];
				$IMG[] = $image;
			}
//		var_dump($IMG);
//			die;
			foreach ($IMG as $val) {
//				global $id;
				$imageInfo=uploaded($val, '../../public/upload');
				//将图片信息了插入数据库中
				if ($imageInfo['error']) {
					//获取图片中路径信息
					$pic = $imageInfo['info'];
					//准备sql语句
					$sql = "insert into goodsimg (url,gid) values('{$pic}','{$id}')";

//					echo $sql;die;
					//连接数据库
					$link = connect();
					//执行插入的操作
					if ($link['error']) {
						//执行操作
						$result = excute($link['mess'], $sql);
//						var_dump($result);die();
						if ($result['error']) {
							echo "<script>alert('添加成功');location.href='./imageList.php?id={$id}'</script>";
							die;
						} else {
							//如果数据插入失败，需要将图片删除
							$path = '../../public/upload/'.$pic;
							@unlink($path);
							echo "<script>alert('请重新添加数据');location.href='./imagelist.php?id={$id}'</script>";
						}
					}
				}
			}
			var_dump($IMG);
		break;
		case 'delimg':
			//接受商品的id
			$id = $_GET['id'];
			$cid=$_GET['cid'];
			//var_dump($id);
			//需要将该条商品的图片信息查询出来
			//连接数据库
			$link = connect();
			//得到旧图的信息
			if ($link['error']) {
				$sql = "select url from goodsimg where id ='{$id}'";
				$result = query($link['mess'], $sql);
			}
			$old_image = $result[0]['url'];
			//执行删除的操作
			if ($link['error']) {
				$sql = "delete from goodsimg where id ='{$id}'";
				$result = excute($link['mess'], $sql);
				//判断是否删除成功
				if ($result['error']) {
					//删除掉旧的图片
					@$path = '../../public/upload/'.$old_image;
					@unlink($path);
					echo "<script>alert('删除成功');location.href='./goodsList.php'</script>";
					die();
				}
			}
			echo "<script>alert('删除失败');location.href='./goodsList.php'</script>";
		break;
	}



?>